package com.rent.ordersinfo.controller;

import java.util.Arrays;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.rent.common.annotation.Log;
import com.rent.common.enums.BusinessType;
import com.rent.ordersinfo.domain.RentOrders;
import com.rent.ordersinfo.service.IRentOrdersService;
import com.rent.common.core.controller.BaseController;
import com.rent.common.core.domain.AjaxResult;
import com.rent.common.utils.poi.ExcelUtil;
import com.rent.common.core.page.TableDataInfo;

/**
 * 订单管理Controller
 * 
 * @author cooper
 * @date 2024-10-15
 */
@Controller
@RequestMapping("/ordersinfo/ordersinfo")
public class RentOrdersController extends BaseController
{
    private String prefix = "ordersinfo/ordersinfo";

    @Autowired
    private IRentOrdersService rentOrdersService;

    @RequiresPermissions("ordersinfo:ordersinfo:view")
    @GetMapping()
    public String ordersinfo()
    {
        return prefix + "/ordersinfo";
    }

    /**
     * 查询订单管理列表
     */
    @RequiresPermissions("ordersinfo:ordersinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RentOrders rentOrders)
    {
        startPage();
        List<RentOrders> list = rentOrdersService.selectRentOrdersList(rentOrders);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @RequiresPermissions("ordersinfo:ordersinfo:export")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RentOrders rentOrders)
    {
        List<RentOrders> list = rentOrdersService.selectRentOrdersList(rentOrders);
        ExcelUtil<RentOrders> util = new ExcelUtil<RentOrders>(RentOrders.class);
        return util.exportExcel(list, "订单管理数据");
    }

    /**
     * 新增订单管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存订单管理
     */
    @RequiresPermissions("ordersinfo:ordersinfo:add")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RentOrders rentOrders)
    {
        return toAjax(rentOrdersService.insertRentOrders(rentOrders));
    }

    /**
     * 修改订单管理
     */
    @RequiresPermissions("ordersinfo:ordersinfo:edit")
    @GetMapping("/edit/{orderId}")
    public String edit(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        RentOrders rentOrders = rentOrdersService.selectRentOrdersByOrderId(orderId);
        mmap.put("rentOrders", rentOrders);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单管理
     */
    @RequiresPermissions("ordersinfo:ordersinfo:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RentOrders rentOrders)
    {
        return toAjax(rentOrdersService.updateRentOrders(rentOrders));
    }

    /**
     * 删除订单管理
     */
    @RequiresPermissions("ordersinfo:ordersinfo:remove")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        // 将字符串转换为 Long 数组
        Long[] idArray = Arrays.stream(ids.split(","))
                .map(String::trim)  // 去除空白字符
                .filter(s -> !s.isEmpty())  // 过滤掉空字符串
                .map(Long::valueOf)
                .toArray(Long[]::new);

        // 调用服务层方法
        return toAjax(rentOrdersService.deleteRentOrdersByOrderIds(idArray));
    }
}
